import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store';

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
    return originalPush.call(this, location).catch(err => {})
}
Vue.use(VueRouter)

const routes = [{
        path: "/",
        redirect: "/home"
    },
    {
        path: '/home',
        name: 'home',
        component: HomeView
    },
    {
        path: '/goods',
        name: 'goods',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "goods" */ '../views/Goods.vue')
    },
    {
        path: '/user',
        name: 'user',
        component: () =>
            import ( /* webpackChunkName: "user" */ '../views/User.vue'),
        redirect: "/user/cart",
        children: [{
                path: "cart",
                component: () =>
                    import ( /* webpackChunkName: "cart" */ '../components/user/Cart.vue')
            },
            {
                path: "profiles",
                component: () =>
                    import ( /* webpackChunkName: "profiles" */ '../components/user/Profiles.vue')
            }
        ]
    },
    {
        path: '/order',
        name: 'order',
        component: () =>
            import ( /* webpackChunkName: "order" */ '../views/Order.vue')
    },
    {
        path: '/free',
        name: 'free',
        component: () =>
            import ( /* webpackChunkName: "free" */ '../views/Free.vue')
    },
    {
        path: '/details',
        name: 'details',
        component: () =>
            import ( /* webpackChunkName: "details" */ '../views/Details.vue')
    },
    {
        path: '*',
        name: 'Error',
        component: () =>
            import ( /* webpackChunkName: "error" */ '../views/Error.vue')
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// router.beforeEach((to, from, next) => {
//     //每一次路由的跳转都执行这里代码
//     // to 要去往的路由对象 新
//     // from  要发送跳转的路由对象 旧
//     // next()表示发行，必须执行这个函数才能跳转到其他路由否则无法跳转
//     //业务需要：没有登录的时候不能去往个人中心

//     //先判断是不是去个人中心
//     //然后判断是否已经登录，有登录就发行，没登录就提示请先登录，然后不发行（调用next()）
//     if (to.path === "/user") {
//         let token = localStorage.getItem("x-auth-token");
//         if (token) {
//             next()
//         } else {
//             store.dispatch("ToastStatus/asyncChanIsShowToast", {
//                 msg: "请先登录！！！",
//                 type: "warning"
//             })
//         }
//         return
//     }
//     next()
// })

export default router